/**
 * 动态字典加载与转译处理方法
 */
import {
  axios,
} from "../main"
/**
 * 加载字典数据
 * @param {*} baseUrl  服务根地址
 * @param {*} paramsStr 字段参数 eg:name=beijing&area=haidian...
 * @param {*} dictkind 字典类型
 */
function loadDict(baseUrl, paramsStr, dictkind) {
  if (paramsStr == null) {
    paramsStr = "";
  } else {
    paramsStr = "?" + paramsStr;
  }
  return new Promise((resolve, reject) => {
    axios.get(baseUrl + "rest/pub/dict/tolist" + paramsStr, {
      params: {
        kind: dictkind
      }
    }).then((res) => {
      if (res.data.result == 'true') {
        resolve(res.data.resultData);
      }
    }).catch(function (error) {
      reject(error);
    });
  })
}

/**
 * 字典转译
 * @param {*} dictkinddata  字典类型数据
 * @param {*} valu  需转译的字典代码值
 * @param {*} fieldCode  字典代码字段 默认code
 * @param {*} fieldCaption  字典标题字段 默认name
 */
function transDict(dictkinddata, fieldCode, fieldCaption, valu) {
  if (fieldCode == null || fieldCode == '') {
    fieldCode = 'code';
  }
  if (fieldCaption == null || fieldCaption == '') {
    fieldCaption = 'name';
  }
  if (dictkinddata != null && dictkinddata != undefined) {
    for (var i = 0; i < dictkinddata.length; i++) {
      var itm = dictkinddata[i];
      if (itm[fieldCode] == valu) {
        return itm[fieldCaption];
      }
    }
  }
  return valu;
}


/**
 * 加载级联字典数据
 * @param {*} baseUrl  服务根地址
 * @param {*} paramsStr 字段参数 eg:name=beijing&area=haidian...
 * @param {*} dictkind 字典类型
 */
function loadCascade(baseUrl, paramsStr, dictkind) {
  if (paramsStr == null) {
    paramsStr = "";
  } else {
    paramsStr = "?" + paramsStr;
  }
  return new Promise((resolve, reject) => {
    axios.get(baseUrl + "rest/pub/dict/tolist" + paramsStr, {
      params: {
        kind: dictkind
      }
    }).then((res) => {
      if (res.data.result == 'true') {
        resolve(res.data.resultData);
      } else {}
    }).catch(function (error) {
      reject(error);
    });
  })
}

/**
 * 递归查找级联字典值
 * @param {*} pnode 
 * @param {*} fid 
 * @param {*} pids 
 */
function findCascadeNode(pnode, labelvalue, labelname, pids, getvalue) {
  if (!getvalue) {
    pids += "," + pnode[labelname];
  } else {
    pids += "," + pnode[getvalue];
  }
  if (pnode[labelname] == labelvalue) {
    return pids;
  } else {
    if (('children' in pnode)) {
      for (var i = 0; i < pnode.children.length; i++) {
        var ids = findCascadeNode(pnode.children[i], labelvalue, labelname, pids, getvalue);
        if (ids != null && ids != 'null' && ids != 'undefined') {
          return ids;
        }
      }
    }
  }
  return null;
}

/**
 * 通用方法  根据字典值 重置级联字典 的显示内容
 */
function resetCascadeValue(dictkinddata, labelname, labelvalu, getvalue) {
  for (var i = 0; i < dictkinddata.length; i++) {
    var ids = findCascadeNode(dictkinddata[i], labelvalu, labelname, '', getvalue);
    if (ids != null && ids != '' && ids != 'null' && ids != 'undefined') {
      ids = ids.substring(1);
      var res = ids.split(',');
      return res;
    }
  }
  return [];
}

export default {
  loadDict: loadDict,
  transDict: transDict,
  loadCascade: loadCascade,
  resetCascadeValue: resetCascadeValue,

}